#include "ServerDialog.h"
#include "ui_ServerDialog.h"

ServerDialog::ServerDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ServerDialog)
{
    ui->setupUi(this);
    serverNum = 0;
    for(int i = 0; i < 10; i++)
    {
        first_name[i] = 0;
    }
    connect (&tcpServer, SIGNAL(newConnection()),this, SLOT(on_Newconnection()));
}

ServerDialog::~ServerDialog()
{
    delete ui;
}

//创建服务器
void ServerDialog::on_CreateButton_clicked()
{
    port = ui->PortEdit->text().toShort();
    bool res = tcpServer.listen(QHostAddress::Any, port);
    if(res==false)
    {
        qDebug("创建失败");
    }
    qDebug("创建成功");
    ui->CreateButton->setEnabled(false);
    ui->PortEdit->setEnabled(false);
}

void ServerDialog::on_Newconnection(void)
{
    //获取和客户端通信的套接字
        QTcpSocket *tcpClientSocket = tcpServer.nextPendingConnection();
        //将获取到的套接字保存到容器中
        tcpClientList.append(tcpClientSocket);

        serverNum++;
        connect (tcpClientSocket, SIGNAL(readyRead()),this, SLOT(on_ReadyRead()));
}

void ServerDialog::on_ReadyRead(void)
{
    QString str;
    msg = "";
 //判断套接字是否已经断开连接, 如果断开则删除
    for (int i = 0; i < tcpClientList.size(); i++)
    {
        if (tcpClientList.at(i)->state() == QAbstractSocket::UnconnectedState)
        {
            tcpClientList.removeAt(i);
            first_name[i] = 0;
            for(int j = i; j < serverNum-1; j++)
            {
                name[j] = name[j+1];
            }
            serverNum--;
        }
    }

    //遍历检查哪个和客户端通信的套接字上面有等待读取的数据
    for(int i=0; i<tcpClientList.size(); i++)
    {
        if(tcpClientList.at(i)->bytesAvailable())
        {
            QByteArray readBuf = tcpClientList.at(i)->readAll();
            ui->ServerList->addItem(readBuf);
            ui->ServerList->scrollToBottom();
            if(first_name[i] == 0)
            {
                name[i] = readBuf;
                first_name[i]++;
                qDebug() << name[i];
                qDebug() << first_name[i];
                QString str = readBuf + ":进入聊天室";
                readBuf = str.toUtf8();
                for (int i = 0; i < tcpClientList.size(); i++)
                {
                    tcpClientList.at(i)->write(readBuf);
                }
            }
            nameSend = name[i];
            msg = readBuf;
            if(msg[0] == 'q') sendQMessage(readBuf);
            if(msg[0] == 's') sendSMessage(readBuf);
        }
    }
}
void ServerDialog::sendQMessage (const QByteArray& readbuf)
{
    QString at_name;
    for(int i = 1; i < 8; i++)
    {
        if(msg[i] == ' ') break;
        at_name[i] = msg[i];
    }
    QString str = at_name+":"+msg.mid(9, msg.size());
    QByteArray buf = str.toUtf8();
    for (int i = 0; i < tcpClientList.size(); i++)
    {
        tcpClientList.at(i)->write(buf);
    }   
}
void ServerDialog::sendSMessage (const QByteArray& readbuf)
{
    int i = 0;
    QString at_name;
    for(int i = 1; i < 8; i++)
    {
        if(msg[i] == ' ')   break;
        at_name[i-1] = msg[i];
    }
    qDebug() << at_name;
    for(i = 0; i < serverNum; i++)
    {
        if(QString::compare(at_name, name[i]) == 0)
        {
            QString str = nameSend+"(私):"+msg.mid(9, msg.size());
            QByteArray buf = str.toUtf8();
            tcpClientList.at(i)->write(buf);
            break;
        }
    }

}








